package com.yf.course.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.log.annon.LogInject;
import com.yf.ability.log.enums.LogType;
import com.yf.base.api.annon.DataProtect;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.BaseStateReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.course.modules.course.dto.CourseDTO;
import com.yf.course.modules.course.dto.CourseFileDTO;
import com.yf.course.modules.course.dto.KnowladgeDTO;
import com.yf.course.modules.course.dto.ext.CourseDetailExtDTO;
import com.yf.course.modules.course.dto.request.CourseNotifyReqDTO;
import com.yf.course.modules.course.dto.request.UserCourseReqDTO;
import com.yf.course.modules.course.dto.response.UserCourseRespDTO;
import com.yf.course.modules.course.entity.Course;
import com.yf.course.modules.course.service.CourseFileService;
import com.yf.course.modules.course.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags={"openAPI课程信息"})
@RestController
@RequestMapping("/api/openAI/course/course")
public class OpenApiCourseController extends BaseController{
    @Autowired
    private CourseService baseService;

    @Autowired
    private CourseFileService courseFileService;

    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @LogInject(title="添加或修改课程", logType = LogType.COURSE)
    @DataProtect(clazz = Course.class, update = true)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest save(@RequestBody CourseDetailExtDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }


    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @LogInject(title="添加或修改课程(exit Id)", logType = LogType.COURSE)
    @DataProtect(clazz = Course.class, update = true)
    @ApiOperation(value = "添加或修改(exit Id)")
    @PostMapping("/saveById")
    public ApiRest saveById(@RequestBody CourseDetailExtDTO reqDTO) {
        String Id = baseService.saveById(reqDTO);
        return super.success(Id);
    }

    /**
     * 批量删除
     * @param reqDTO
     * @return
     */
    @LogInject(title="批量删除课程", logType = LogType.COURSE)
    @DataProtect(clazz = Course.class, delete = true)
    @ApiOperation(value = "批量删除")
    @PostMapping("/delete")
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
     * 查找详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<CourseDetailExtDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        CourseDetailExtDTO dto = baseService.detail(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 简略的详情，用于一些简单的地方
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @PostMapping("/simple-detail")
    public ApiRest<CourseDTO> simpleDetail(@RequestBody BaseIdReqDTO reqDTO) {
        Course course = baseService.getById(reqDTO.getId());
        CourseDTO respDTO = new CourseDTO();
        BeanMapper.copy(course, respDTO);
        return super.success(respDTO);
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<CourseDTO>> paging(@RequestBody PagingReqDTO<CourseDTO> reqDTO) {
        //分页查询并转换
        IPage<CourseDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }


    /**
     * 修改状态
     * @param reqDTO
     * @return
     */
    @LogInject(title="启用或禁用课程", logType = LogType.COURSE)
    @ApiOperation(value = "修改状态")
    @PostMapping("/state")
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {

        // 条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(Course::getId, reqDTO.getIds());

        Course record = new Course();
        record.setState(reqDTO.getState());
        baseService.update(record, wrapper);
        return super.success();
    }


    /**
     * 查找用户课程学习情况
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/user-paging")
    public ApiRest<IPage<UserCourseRespDTO>> userPaging(@RequestBody PagingReqDTO<UserCourseReqDTO> reqDTO) {
        //分页查询并转换
        IPage<UserCourseRespDTO> page = baseService.userPaging(reqDTO);
        return super.success(page);
    }


    /**
     * 发送学习安排通知
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "发送学习安排通知")
    @PostMapping("/prepare-notify")
    public ApiRest notify(@RequestBody CourseNotifyReqDTO reqDTO) {
        baseService.prepareNotify(reqDTO);
        return super.success();
    }

    @ApiOperation(value = "更新课件上传完成状态",notes = "更新课件上传完成状态")
    @PostMapping("/update-fileStatus")
    public ApiRest updateFileStatus(@RequestBody CourseFileDTO reqDTO) {
        courseFileService.updateFileStatus(reqDTO);
        return super.success();
    }

    @ApiOperation(value = "更新课件上传完成状态",notes = "更新课件上传完成状态")
    @PostMapping("/addKnowledgePoint")
    public ApiRest addKnowledgePoint(@RequestBody KnowladgeDTO knowledgeDTO) {
        courseFileService.addKnowledgePoint(knowledgeDTO);
        return super.success();
    }

}
